import {mapMutations, mapGetters} from 'vuex'
let userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
let oId = JSON.parse(sessionStorage.getItem('oid'))
import {
  createDoc,
  addDocProcessReviewObj,
  startDocWfProcess,
  getFolderTree,
  updateDoc,
  getAttributeGroup
} from "@/api/fileSingle.js" 
export default {
  computed: {
    ...mapGetters({
      'copyFileSingleTableData': 'copyFileSingleTableData',
      'fileSingleData': 'fileSingle'
    })
  },
  data() {
    return {
      initTreeArr: [],
      userinfo:JSON.parse(sessionStorage.getItem('userInfo'))
    }
  },
  methods: {
    ...mapMutations(['SET_COPYFILESINGLETABLEDATA', 'SET_FILESINGLE', 'SET_FILESINGLETABLEITEM']),
    setFileSingle(data) {
      this.$store.commit("SET_FILESINGLE", data)
    },
    setFileSingleTableItem(data) {
      this.$store.commit("SET_FILESINGLETABLEITEM", data)
    },
    // 复制
    copy(data) {
      console.log('复制的数据--------',data)
      sessionStorage.setItem('coprOrParseEvent',1)
      this.$message.success('复制成功.....');
      this.$store.commit("SET_COPYFILESINGLETABLEDATA", data)
    },
    // 剪切
    cut(data) {
      console.log('剪切的数据--------',data)
      sessionStorage.setItem('coprOrParseEvent',2)
      this.$message.success('剪切成功.....');
      this.$store.commit("SET_COPYFILESINGLETABLEDATA", data)
    },
    // 粘贴
    paste() {
      return this.copyFileSingleTableData
    },
    // 上传公共方法
    upFile(data) {
      let {types,trees, fileArr, defaultArr, rules, effectKey, otherObj, callback} = data
      let {startSingleID} = otherObj
      for (let key in rules) {
        if (rules[key]) return this.$message.error(key)
      }
      let params = new FormData()
      fileArr.forEach((item, index) => {
        params.append((index + 1) + '_file', fileArr[index])
      })
      params.append('folderOid', trees.length > 1 ? trees[trees.length - 1] : defaultArr[defaultArr.length - 1])
      params.append('userId', this.userinfo.loginName)
      params.append('docType', types)
      createDoc(params).then((res) => {
        if (res && res.data.success) {
          if (effectKey === 'addDocProcessReviewObj') {
            this.addDocProcessReviewObj({
              data: res.data.data, oid: startSingleID, callback: (res1) => {
                return callback(res1)
              }
            })
          }
          if (effectKey === 'callbackData') {
            return callback({code: 200, data: res.data.data})
          }
          if (effectKey === 'checkinObj') {
            return callback(res.data.data)
          }
          if (effectKey === 'addFileStart') {
            return callback(res.data.data)
          }
        } else {
          this.$message.error(res.data.message)
          return callback({code: 404})
        }
      })
    },
    // addDocProcessReviewObj
    addDocProcessReviewObj(callbackObj) {
      let {rules, data, oid, callback} = callbackObj
      if (rules) {
        for (let key in rules) {
          if (rules[key]) return this.$message.error(key)
        }
      }
      let obj = {userId: this.userinfo.loginName, oid: oid, reviewObjOids: []}
      data.forEach(item => {
        obj.reviewObjOids.push(item.oid)
      })
      obj.reviewObjOids = JSON.stringify(obj.reviewObjOids)
      addDocProcessReviewObj(obj).then((res) => {
        if (res && res.data.success) {
          this.$message.success('添加成功.....');
          return callback({code: 200, initTableData: true})
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 获取首页左侧状态数
    getTreeData(callbackObj) {
      let {callback} = callbackObj
      let sessionOid = JSON.parse(sessionStorage.getItem('oid'))
      let data = {userId: this.userinfo.loginName, oid: 'OR:com.pisx.pmgt.project.PIProject:' + sessionOid}
      getFolderTree(data).then((res) => {
        if (res && res.data.success) {
          let arr = res.data.data
          return callback({code: 200, data: arr})
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 检入上传
    checkInUpFile(callbackObj) {
      let {fileArr, rules, otherObj, callback} = callbackObj
      let {oid} = otherObj
      for (let key in rules) {
        if (rules[key]) return this.$message.error(key)
      }
      let params = new FormData()
      fileArr.forEach((item, index) => {
        params.append((index + 1) + '_file', fileArr[index])
      })
      params.append('oid', oid)
      params.append('userId', this.userinfo.loginName)
      updateDoc(params).then((res) => {
        if (res && res.data.success) {
          return callback({code: 200, data: res.data.data})
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 获取oid公共方法
    cycleID(arr, otherObj) {
      let r = []
      arr.forEach(item => {
        if (otherObj) {
          console.log(123)
        } else {
          if(item.oid){
            r.push(item.oid)
          }else if(item.linkOid){
            r.push(item.linkOid)
          }else{
            r.push(item)
          }
        }
      })
      return r
    },
    // 获取复制的文件oid传参
    copyOids(arr) {
      let r = [], objR = {}
      arr.forEach(item => {
        objR = new Object()
        objR.oid = item.oid
        objR.newName = ''
        r.push(objR)
      })
      return r
    },
    /**
     * 启动文档签审公共方法
     */
    startDocWfProcess(callbackObj) {
      let {callback} = callbackObj
      let data = {oids: [], processTemplate: 'PIDocSignWf', processUsers: '', processState: ''}
      this.fileSingleData.tableArr.forEach(item => {
        data.oids.push(item.oid + '')
      })
      data.oids = JSON.stringify(data.oids)
      startDocWfProcess(data).then((res) => {
        if (res && res.data.success) {
          this.fileSingleData.startSingleID = res.data.data ? res.data.data[0] : ''
          this.setFileSingle(this.fileSingleData)
          this.$router.push('fileSingle/fileStart');
          return callback({code: 200})
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    /**
     * 获取基本信息
     */
    getBasicInfo(callbackObj) {
      let {tableArr, callback} = callbackObj
      if (tableArr.length < 1) return
      getAttributeGroup({userId: this.userinfo.loginName, oid: tableArr[0].oid}).then((res) => {
        if (res && res.data.success) {
          let arr = res.data.data[0].rows
          return callback({code: 200, data: arr})
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  },
}